import random

from task0515.re_blackjack import Player, Poker


class BlackJackPlayer(Player):

    def __init__(self, name, *, money=0, is_banker=False):
        self.money = money
        self.is_banker = is_banker
        super().__init__(name)

    def throw_cards(self):
        self.cards.clear()

    def win(self, curr_stake):
        self.money += 2 * curr_stake if self.current_point == 21 else curr_stake

    def lose(self, curr_stake):
        self.money -= curr_stake

    @property
    def current_point(self):
        sorted_cards = sorted(self.cards, key=lambda curr_card: curr_card.face, reverse=True)
        total = 0
        for card in sorted_cards:
            if card.face >= 10:
                total += 10
            elif card.face > 1:
                total += card.face
            else:
                total += 1 if total + 11 > 21 else 11
        return total


def reset_game(poker, players):
    poker.shuffle()
    for player in players:
        player.throw_cards()
    for _ in range(2):
        for player in players:
            player.get_one((poker.send()))
    for player in players:
        display_cards(player, show_all=False)


def display_cards(player, *, show_all=True):
    print(f'{player.name}:')

    for index, cards in enumerate(player.cards):
        if index == 0 and player.is_banker and not show_all:
            print('⚑', end='')
        else:
            print(cards, end='')
    print(f'{player.current_point}点' if show_all else '')


def main():
    poker = Poker()
    player1 = BlackJackPlayer('huang', money=1000)
    player2 = BlackJackPlayer('pc', is_banker=True)
    while player1.money > 0:
        print(f'玩家{player1.name}总资产为：{player1.money}')
        curr_stack = 0
        while curr_stack <= 0 or curr_stack > player1.money:
            curr_stack = int(input("清下注:"))
        reset_game(poker, (player1, player2))
        blow_up = False
        while True:
            choice = input('要牌吗？')
            if choice.lower() in ('y', 'yes', 'yeah'):
                player1.get_one((poker.send()))
                display_cards(player1, show_all=False)
                if player1.current_point > 21:
                    blow_up = True
                    print(f'玩家{player1.name}爆了')
                    player1.lose(curr_stack)
                    break
            else:
                break
        if not blow_up:
            min_expected_point = 15 + random.randint(0, 4)
            while player2.current_point < min_expected_point:
                player2.get_one(poker.send())
                if player2.current_point > 21:
                    print(f'庄家{player2.name}爆了!!')
                    display_cards(player2)
                    player1.win(curr_stack)
                    break
        if not blow_up:
            display_cards(player1)
            display_cards(player2)
            if player1.current_point > player2.current_point:
                player1.win(curr_stack)
            else:
                player1.lose(curr_stack)
    print("你破产了，游戏结束！！")


if __name__ == '__main__':
    main()
